package cpa.prowon.gui.actions;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.MessageBox;

import pho.prowon.category.RootIndexCategory;
import cpa.prowon.Application;
import cpa.prowon.gui.MainWindow;

/**
 * This action is used to clear the whole index. Before the index gets actually
 * cleared, the user is asked whether or not he is really sure to do that
 * @author Chris
 */
public class ClearIndexAction extends PatchedAction {

	/**
	 * Constructor
	 * @param MainWindow The window where the action is sued
	 * @author Chris
	 */
	public ClearIndexAction(MainWindow w) {
		super(w);
	}

	/**
	 * Return the title key
	 * @return The title key
	 * @author Chris
	 */
	protected String getTitleKey() {
		return "menu_item_file_clearindex";
	}

	/**
	 * Return the tooltip key
	 * @return The tooltip key
	 * @author Chris
	 */
	protected String getTooltipKey() {
		return "";
	}

	/**
	 * Return the image key
	 * @return The image key
	 * @author Chris
	 */
	protected String getImageKey() {
		return "";
	}
	
	/**
	 * Run the action
	 * @author Chris
	 */
	public void run() {

		// Ask the user whether he is really sure to clear the index
		MessageBox msgBox = new MessageBox(window.getShell(), SWT.OK | SWT.CANCEL);
		msgBox.setText(Application.getInstance().getResourceBundle().getString("title_clearindex"));
		msgBox.setMessage(Application.getInstance().getResourceBundle().getString("msg_wantclearindex"));
		int answer = msgBox.open();
		
		// If the user has chosen no, then return
		if (answer != SWT.OK) {
			return;
		}
		
		// Clear the current index
		RootIndexCategory rootIndexCat = window.getCategoryPart().getRootIndexCategory();
		rootIndexCat.clear();
		
		// Refresh the viewers
		window.refreshCategoryPart();
		window.refreshListPart();
		
	}
	
}
